package com.smartum.mpr.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.model.UploadedImage;
import com.smartum.mpr.model.UploadedImageStatus;

@Repository
public class UploadedImageDao extends GenericDao<Long, UploadedImage> {

	@Override
	protected Class<UploadedImage> getEntityClass() {
		return UploadedImage.class;
	}

	public void declineImages(Long[] images) {
		StringBuffer query = new StringBuffer("update UploadedImage  SET status=2 WHERE id IN(");
		for (int i = 0; i < images.length; i++) {
			query.append(images[i]);
			if (i < images.length - 1) {
				query.append(", ");
			}
		}
		query.append(")");
		hibernateTemplate.bulkUpdate(query.toString());
	}

	public void confirmImages(Long[] images) {
		StringBuffer query = new StringBuffer("update UploadedImage  SET status=1 WHERE id IN(");
		for (int i = 0; i < images.length; i++) {
			query.append(images[i]);
			if (i < images.length - 1) {
				query.append(", ");
			}
		}
		query.append(")");
		hibernateTemplate.bulkUpdate(query.toString());
	}

	public List<UploadedImage> getPublicUploadedImages(Long timestamp, int amount) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImage.class);
		criteria.addOrder(Order.desc("imageTimestamp"));
		if (timestamp == 0) {
			criteria.add(Restrictions.gt("imageTimestamp", timestamp));
		} else {
			criteria.add(Restrictions.lt("imageTimestamp", timestamp));

		}
		System.out.println("recupere imagenes getPublicUploadedImages");
		return hibernateTemplate.findByCriteria(criteria, 0, amount);
	}

	public List<UploadedImage> getUserUploadedImages(Long timestamp, int amount, long userId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImage.class);
		criteria.addOrder(Order.desc("imageTimestamp"));
		if (timestamp == 0) {
			criteria.add(Restrictions.gt("imageTimestamp", timestamp)).createCriteria("owner")
			        .add(Restrictions.eq("id", userId));
		} else {
			criteria.add(Restrictions.lt("imageTimestamp", timestamp)).createCriteria("owner")
			        .add(Restrictions.eq("id", userId));
		}
		System.out.println("recupere imagenes getUserUploadedImages");
		return hibernateTemplate.findByCriteria(criteria, 0, amount);
	}

	public List<UploadedImage> getUnconfirmedImages(Long timestamp, int amount) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImage.class);
		criteria.addOrder(Order.desc("imageTimestamp"));
		criteria.add(Restrictions.eq("status", UploadedImageStatus.NOT_MODERATED));
		if (timestamp == 0) {
			criteria.add(Restrictions.gt("imageTimestamp", timestamp));
		} else {
			criteria.add(Restrictions.lt("imageTimestamp", timestamp));
		}

		return hibernateTemplate.findByCriteria(criteria, 0, amount);
	}

	public long getUnconfirmedImagesNumber() {
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImage.class);
		criteria.add(Restrictions.eq("status", UploadedImageStatus.NOT_MODERATED));

		return hibernateTemplate.findByCriteria(criteria).size();
	}

}
